/**
 * (C) Copyright Talent Information Solutions Ltd.
 *
 * Class Name:	ToolKit
 *
 * Aim:	工具箱 ：零散功能的集合
 *
 * Creation Date:	2003.03.11
 *
 * @Author:	Chenyj
 * @version:	1.0
 *
 * Modification History
 *
 * Reference	Date		Modified By	Reason
 * ---------	----		-----------	------
 */
package com.yjt.common.tools;

import java.util.Properties;

public class ToolKit {

	public ToolKit() {

	}

	private final static char[] charSet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
			'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
			'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
			'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
			'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9' };

	private final static int CHAR_NUMBER_SETS_LEN = 62;

	/**
	 * 将字符串以MD5算法加密
	 * 
	 * @param OrigString
	 * @return
	 */
	public static String encryptMD5(String OrigString) {

		try {

			java.security.MessageDigest alg = java.security.MessageDigest
					.getInstance("MD5");
			alg.update(OrigString.getBytes());

			return new String(alg.digest());

		} catch (java.security.NoSuchAlgorithmException ex) {
			System.out.println("非法摘要算法");
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("非法摘要算法");
			return null;
		}
	}

	/**
	 * 将字符串以MD5算法加密
	 * 
	 * @param OrigString
	 * @return
	 */
	public static String encryptMD5ByteToString(String OrigString) {

		try {
			java.security.MessageDigest alg = java.security.MessageDigest
					.getInstance("MD5");
			alg.update(OrigString.getBytes());

			String str = byteToString(alg.digest());

			Properties props = new Properties();
			props.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("/efss.properties"));
			String charset = props.getProperty("DB_CHARSET");
			if (charset == null || charset.trim().equals("")) {
				return str;
			} else {
				String aa = new String(str.getBytes(), charset);
				return aa;
			}

		} catch (java.security.NoSuchAlgorithmException ex) {
			System.out.println("非法摘要算法");
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("非法摘要算法");
			return null;
		}
	}

	/**
	 * 将字符串以MD5算法加密，返回32位 16进制的字符串
	 * 
	 * @param OrigString
	 * @return
	 */
	public static String encryptMD5HEX(String OrigString) {
		try {
			java.security.MessageDigest alg = java.security.MessageDigest
					.getInstance("MD5");
			alg.update(OrigString.getBytes());

			return byte2hex(alg.digest());
		} catch (java.security.NoSuchAlgorithmException ex) {
			System.out.println("非法摘要算法");
			return null;
		}
	}

	/**
	 * 将二进制的数值串转换为16进制的字符串
	 * 
	 * @param byteString
	 *            二进制的数值串
	 * @return String 16进制的字符串
	 */
	public static String byte2hex(byte[] byteString) {

		String result = "";
		String tmpChar = "";

		for (int n = 0; n < byteString.length; n++) {

			tmpChar = (java.lang.Integer.toHexString(byteString[n] & 0XFF));

			if (tmpChar.length() == 1) {
				result = result + "0" + tmpChar;
			} else {
				result = result + tmpChar;
			}
		}
		return result.toUpperCase();
	}

	/**
	 * 改进的冒泡算法
	 * 
	 * @param obj
	 *            对象组 对该组对象进行排序
	 */
	public static void betterBubbleSort(Object[] obj) {

		int len = obj.length;

		int i, t, bound;

		int tmpFirst, tmpSecond;

		Object tmpObj;

		t = len - 1;

		while (t > 0) {
			bound = t;
			t = 0;
			for (i = 0; i < bound; i++) {
				tmpFirst = Integer.parseInt(obj[i].toString());
				tmpSecond = Integer.parseInt(obj[i + 1].toString());
				if (tmpFirst > tmpSecond) {
					tmpObj = obj[i];
					obj[i] = obj[i + 1];
					obj[i + 1] = tmpObj;
					t = i;
				}
			} // end of for
		}// end of while

	}// end of BetterBubbleSort()

	public static void betterBubbleSortS(Object[] obj) {

		int len = obj.length;

		int i, t, bound;

		String tmpFirst, tmpSecond;

		Object tmpObj;

		t = len - 1;

		while (t > 0) {
			bound = t;
			t = 0;
			for (i = 0; i < bound; i++) {
				tmpFirst = obj[i].toString();
				tmpSecond = obj[i + 1].toString();
				if (tmpFirst.compareTo(tmpSecond) > 0) {
					tmpObj = obj[i];
					obj[i] = obj[i + 1];
					obj[i + 1] = tmpObj;
					t = i;
				}
			} // end of for
		}// end of while

	}// end of BetterBubbleSort()

	/**
	 * 随机字符串 （含数字、字母）可作为随机密码
	 * 
	 * @param obj
	 *            对象组 对该组对象进行排序
	 */
	public static String RandomString(int stringLen) {

		char[] randChars = new char[stringLen];
		int i = 0;
		int position = 0;

		for (; i < stringLen; i++) {
			position = (int) java.lang.Math.round(java.lang.Math.random()
					* CHAR_NUMBER_SETS_LEN);
			position = position != 0 ? position - 1 : position;
			randChars[i] = charSet[position];
		}

		return new String(randChars);

	}// end of RandomString()

	/**
	 * 随机字符串 （含数字、字母）可作为随机密码
	 * 
	 * @param obj
	 *            对象组 对该组对象进行排序
	 */
	public static String getNowTime(String format) {

		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);

		return sdf.format(java.util.Calendar.getInstance().getTime());

	} // End of getNowTime()

	/**
	 * 改变日期显示格式 ####-##-##
	 * 
	 * @param date
	 * @return
	 */
	public static String changeDataFormat(String date) {

		return date.substring(0, 4) + "-" + date.substring(4, 6) + "-"
				+ date.substring(6, 8);
	}

	public static String dateFormat(String date) {
		if (date == null) {
			return "";
		}

		switch (date.trim().length()) {
		case 8:
			// return date.trim().substring(0,4) + "." +
			// date.trim().substring(4,6) + "." + date.trim().substring(6,8);
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6);
		case 7:
			// return date.trim().substring(0,4) + "." +
			// date.trim().substring(4,6) + "." + date.trim().substring(6,8);
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6);
		case 6:
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6);
		case 5:
			return date.trim().substring(0, 4) + ".0"
					+ date.trim().substring(4, 5);
		case 4:
			return date.trim().substring(0, 4);
		default:
			return date.trim();
		}
	}

	public static String dateFormat(String date, int len) {
		if (date == null) {
			return "";
		}
		if (len > date.trim().length()) {
			len = date.trim().length();
		}

		switch (len) {
		case 8:
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6);
		case 6:
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6);
		case 5:
			return date.trim().substring(0, 4) + ".0"
					+ date.trim().substring(4, 5);
		case 4:
			return date.trim().substring(0, 4);
		default:
			return date.trim();
		}

	}

	public static String dateFullFormat(String date) {
		if (date == null) {
			return "";
		}

		switch (date.trim().length()) {
		case 8:
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6) + "."
					+ date.trim().substring(6, 8);
		case 6:
			return date.trim().substring(0, 4) + "."
					+ date.trim().substring(4, 6);
		case 5:
			return date.trim().substring(0, 4) + ".0"
					+ date.trim().substring(4, 5);
		case 4:
			return date.trim().substring(0, 4);
		default:
			return date.trim();
		}
	}

	/**
	 * 判断字符串是否为null，如果是则返回""，否则返回原串
	 * 
	 * @param str
	 * @return
	 */
	public static String adjustNull(String str) {
		return str == null ? "" : str.trim();
	}

	public static boolean isAllLetter(String str) {
		int i_length = str.length();
		int i_loop = 0;
		char c;

		if (i_length == 0)
			return false;

		while (i_loop < i_length) {
			// if(!Character.isLetter(str.charAt(i_loop)))
			// return false;
			// i_loop++;
			c = str.charAt(i_loop);
			if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')
				i_loop++;
			else
				return false;
		}
		return true;
	}

	public static char byteToChar(byte[] b) {
		int s = 0;
		if (b[0] > 0)
			s += b[0];
		else
			s += 256 + b[0];

		s *= 256;

		if (b[1] > 0)
			s += b[1];
		else
			s += 256 + b[1];

		char ch = (char) s;

		return ch;
	}

	public static String byteToString(byte[] bytes) {
		int len = bytes.length / 2;
		char[] charTmp = new char[len];
		byte[] byteTmp = new byte[2];
		for (int i = 0; i < len; i++) {
			byteTmp[0] = bytes[i * 2];
			byteTmp[1] = bytes[i * 2 + 1];
			charTmp[i] = byteToChar(byteTmp);
		}

		return new String(charTmp);
	}
/*
	public static void main(String[] args) {
		try {
			String msg = "GRwH1zEFGcRDBIjMAWabgywGyCufrl8GhEfv46vXDA6DTUzb6es1m7dLev7u9ylv22TawHxzKcEFkuaqI+0SAc/DHqt2Ggeg7AsWtNqjlWGk98fKRZNqTYfSAWfomaYM7QJeEE6iLjM/aEjvYV4ka9Z1h5JLz7xvqMT7RBUgV8g=";

			// String enc = encryptMD5HEX(msg);

			//String dec = encryptMD5(msg);

			//System.out.println("[enc " + dec.getBytes().length + "] : " + dec);
			// System.out.println("[enc " + enc.length() + "] : " + enc);
			
			//System.out.println(Hex.encodeHexString(msg.getBytes("GBK")));
			//System.out.println(Hex.encodeHexString(msg.getBytes("UTF-8")));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
*/
}
